class Cow {
  constructor(age) {
    this.age = age
    this.isAlive = true
  }
  grow() {
    this.age += 1
    if (this.age > 15) {
      this.isAlive = false
    }
  }
  isCanBirth() {
    if (this.age < 5 || !this.getAlive()) {
      return null
    }
    return new Cow(0)
  }
  getAlive() {
    return this.isAlive
  }
}


class From {
  constructor() {
    this.cowList = []
  }
  getCowList() {
    return this.cowList
  }
  setCowList(list) {
    this.cowList = list
  }
}

class Main {
  constructor() {
    this.form = new From()
    this.initForm()
    // this.grow()
  }
  initForm() {
    let cow = new Cow(5)
    this.form.getCowList().push(cow)
  }
  grow() {
    const cowLists = this.form.getCowList()
    for (let i = 0; i < 20; i++) {
      let dead_num = 0
      let newArr = []
      cowLists.forEach((cow,index)=>{
        cow.grow()
        const child = cow.isCanBirth()
        if (child!=null) {
          newArr.push(child)
        }
        if (!cow.getAlive()) {
          dead_num++
          // cowLists.splice(index,1)
        }
      })
      this.form.getCowList().push(...newArr)
      console.log(`第${i}年，出生${newArr.length}头，死亡${dead_num}头`)
    }
    // 数组删除元素的时候一定要从后向前遍历
    for (let index = cowLists.length-1; index >=0; index--) {
      const cow = cowLists[index];
      if (!cow.getAlive()) {
        cowLists.splice(index,1)
      }
    }
    console.log(cowLists.length)
  }
}

const main_1 = new Main()
main_1.grow()